package xyz.jpcr.covid19app.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import java.util.ArrayList;

import xyz.jpcr.covid19app.Adapter.CityAdapter;
import xyz.jpcr.covid19app.Adapter.ProvinceAdapter;
import xyz.jpcr.covid19app.Model.Country.Cities;
import xyz.jpcr.covid19app.Model.Country.CountryResults;
import xyz.jpcr.covid19app.R;
import xyz.jpcr.covid19app.Util.ILocationUtil;
import xyz.jpcr.covid19app.Util.IStoreUtil;

public class CityWiseActivity extends AppCompatActivity {

    IStoreUtil iStoreUtil = IStoreUtil.getInstance();
    ILocationUtil iLocationUtil = ILocationUtil.getInstance(this);
    Toolbar toolbar;
    RecyclerView recyclerView;
    EditText search;
    CityAdapter cityAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_wise_data);


        toolbar = findViewById(R.id.layoutToolBar);
        recyclerView = findViewById(R.id.citywise_recyclerview);
        search = findViewById(R.id.city_search_editText);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        cityAdapter = new CityAdapter(this, iStoreUtil.getCitiesList());
        recyclerView.setAdapter(cityAdapter);

        toolbar.setTitle(iStoreUtil.getCurrent_province().getProvinceEnglishName());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                filter(s.toString());
            }
        });

    }

    private void filter(String text) {
        ArrayList<Cities> filteredList = new ArrayList<>();
        for(Cities cities : iStoreUtil.getCitiesList()) {
            if(cities.getCityEnglishName().toLowerCase().contains(text.toLowerCase())) {
                filteredList.add(cities);
            }
        }
        cityAdapter.filterList(filteredList);
    }

}